// miniprogram/pages/deviceInfo/index.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    AliasName:'', //设备名称
    deviceInfo:{},  // 设备基本信息
    checked:false,
    switchPowerBool:false,
    deviceStatus:{},  //设备状态数据
    colorControll:[
      { color:'红色', isChecked:false, data:0 },
      { color:'蓝色', isChecked:false, data:2 },
      { color:'绿色', isChecked:false, data:1 }
    ],
    deviceData:{}, //设备物模型数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.getData();
    this.setData({ AliasName:options.AliasName });
    wx.setNavigationBarTitle({
      title: options.AliasName
    });
    this.getDveiceData();
  },
  
  //获取上一页传递来的数据
  getData:function(){
    let that = this;
    const eventChannel = that.getOpenerEventChannel();
    eventChannel.on('getDeviceData',function(data){
      that.setData({
        deviceInfo:data.data.info
      });
      console.log(that.data.deviceInfo);
    })
  },
  
  //开关
  switchPower:function(){    
    let that = this;
    let switchPowerNum;
    let switchPowerBool = that.data.switchPowerBool;
    switchPowerBool = !switchPowerBool;
    if (switchPowerBool) {
      switchPowerNum = 1;
    }else{
      switchPowerNum = 0;
    }
    app.sdk.init().then(()=>{
      let data = { };
      data.DeviceId = that.data.deviceInfo.DeviceId;
      data.Data=`{\"power_switch\":${switchPowerNum}}`;
      app.sdk.requestApi('AppControlDeviceData',data)
       .then((res)=>{
         console.log(res);
       })
       .catch((err)=>{
         console.log(err);
       })
    });
    that.setData({
      switchPowerBool:switchPowerBool
    })
  },

  //颜色按钮
  onChange(e) {
    // 需要手动对 checked 状态进行更新
    // this.setData({ isChecked: e.detail });
    let that = this;
    console.log(e);
    let colorControll = that.data.colorControll;
    colorControll.map(value => {
      if (value.data !== e.currentTarget.dataset.controllnum) {
        value.isChecked = false;
      }
      else if (value.data === e.currentTarget.dataset.controllnum) {
        value.isChecked = e.detail;
      }
    });
    app.sdk.init().then(()=>{
      let data = { };
      data.DeviceId = that.data.deviceInfo.DeviceId;
      data.Data=`{\"color\":${e.currentTarget.dataset.controllnum}}`;
      app.sdk.requestApi('AppControlDeviceData',data)
       .then((res)=>{
         console.log(res);
       })
       .catch((err)=>{
         console.log(err);
       })
    });
    that.setData({
      colorControll:colorControll
    })
  },

  //获取设备详情
  getDetail:async function(DeviceId){
    let that = this;
    let DeviceIdArr = [];
    DeviceIdArr.push(DeviceId);
    app.sdk.init().then(()=>{
      app.sdk.requestApi('AppGetDeviceStatuses',{ DeviceIds:DeviceIdArr  })
      .then((res)=>{
        that.setData({
          deviceStatus:res
        })
      })
      .catch((err)=>{
        console.log(err);
      })
    })
  },

  //获取设备物模型数据
  getDveiceData: function(){
    let that = this;
    app.sdk.init().then(()=>{
      app.sdk.requestApi('AppGetDeviceData',{
        ProductId:that.data.deviceInfo.ProductId,
        DeviceName:that.data.deviceInfo.DeviceName
      })
      .then((result)=>{
        let res = JSON.parse(result.Data);
        console.log(res);
        let colorControll = that.data.colorControll;
        colorControll.map(value => {
          if(value.data === res.color.Value){
            value.isChecked = true;
          }
        });
        let switchPowerBool;
        if(res.power_switch.Value === 1){
          switchPowerBool = true;
        }else if(res.power_switch.Value === 0){
          switchPowerBool = false;
        }
        that.setData({
          deviceData:res,
          colorControll:colorControll,
          switchPowerBool:switchPowerBool
        });
      })
      .catch((err)=>{
        console.log(err);
      })
    })
  },
  


  //跳转到设备详情页
  goToChooseMore:function(e){
    let that = this;
    wx.navigateTo({
      url: `/pages/deviceInfo/detailInfo/index?AliasName=${that.data.AliasName}`,
      success: (result) => {
         result.eventChannel.emit('getDeviceInfo',{ data:e.currentTarget.dataset });
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDetail(this.data.deviceInfo.DeviceId);
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})